using System.ComponentModel.DataAnnotations;

namespace JGSY.CMS.LowCode.Platform.Application.DTOs.Monitoring;

/// <summary>
/// Core Web Vitals 专用指标DTO
/// </summary>
public class VitalsMetricDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public string? UserId { get; set; }

    [Required]
    public string MetricName { get; set; } = string.Empty; // LCP, FID, CLS, FCP, TTFB

    [Required]
    [Range(0, double.MaxValue)]
    public decimal MetricValue { get; set; }

    public string? MetricRating { get; set; } // good, needs-improvement, poor

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? UserAgent { get; set; }
    public string? BrowserName { get; set; }
    public string? DeviceType { get; set; }
    public string? ViewportSize { get; set; } // 新增：视口大小
    public string? ConnectionType { get; set; } // 新增：网络类型

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }

    // Core Web Vitals 特有字段
    public decimal? NavigationStart { get; set; }
    public decimal? DomContentLoaded { get; set; }
    public decimal? LoadComplete { get; set; }
    public string? ElementSelector { get; set; } // LCP 元素选择器
    public decimal? LayoutShiftScore { get; set; } // CLS 具体分数
}

/// <summary>
/// 自定义性能指标DTO
/// </summary>
public class CustomMetricDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public string? UserId { get; set; }

    [Required]
    public string MetricName { get; set; } = string.Empty; // 自定义指标名称

    [Required]
    public decimal MetricValue { get; set; }

    public string? MetricUnit { get; set; } // ms, bytes, count, percentage, etc.
    public string? Category { get; set; } // business, technical, user-experience
    public Dictionary<string, object>? Context { get; set; } // 上下文数据

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? UserAgent { get; set; }
    public string? BrowserName { get; set; }
    public string? DeviceType { get; set; }

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// 页面访问DTO
/// </summary>
public class PageViewDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public string? UserId { get; set; }

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? PageTitle { get; set; }
    public string? Referrer { get; set; }
    public string? UserAgent { get; set; }
    public string? BrowserName { get; set; }
    public string? DeviceType { get; set; }
    public string? Language { get; set; }
    public string? TimezoneOffset { get; set; }
    public Dictionary<string, string>? CustomProperties { get; set; }

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// 点击事件DTO
/// </summary>
public class ClickEventDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? TargetElement { get; set; } // CSS选择器
    public string? ElementText { get; set; }
    public string? ElementType { get; set; } // button, link, input, etc.
    public string? ElementClasses { get; set; }
    public string? ElementId { get; set; }
    
    // 点击位置
    public int? ClickX { get; set; }
    public int? ClickY { get; set; }
    
    // 页面滚动位置
    public int? ScrollX { get; set; }
    public int? ScrollY { get; set; }

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// 滚动深度DTO
/// </summary>
public class ScrollDepthDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    [Required]
    [Range(0, 100)]
    public int ScrollDepthPercent { get; set; } // 滚动深度百分比

    public int MaxScrollDepth { get; set; } // 当前会话最大滚动深度
    public int ViewportHeight { get; set; }
    public int DocumentHeight { get; set; }
    public int ScrollPosition { get; set; }
    public int TimeToScroll { get; set; } // 到达该深度用时（秒）

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// 表单交互DTO
/// </summary>
public class FormInteractionDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    [Required]
    public string InteractionType { get; set; } = string.Empty; // focus, blur, input, submit, abandon

    public string? FormId { get; set; }
    public string? FormName { get; set; }
    public string? FieldName { get; set; }
    public string? FieldType { get; set; }
    public string? FieldValue { get; set; } // 脱敏后的值
    
    // 表单统计
    public int? TotalFields { get; set; }
    public int? CompletedFields { get; set; }
    public int? TimeSpent { get; set; } // 在表单上花费的时间（秒）
    public int? ErrorCount { get; set; } // 验证错误次数

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// JavaScript错误DTO
/// </summary>
public class JavaScriptErrorDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public string? UserId { get; set; }

    [Required]
    public string ErrorMessage { get; set; } = string.Empty;

    public string? ErrorStack { get; set; }
    public string? ErrorName { get; set; } // TypeError, ReferenceError, etc.

    [Required]
    public string ErrorFilename { get; set; } = string.Empty;

    [Required]
    public int ErrorLineNumber { get; set; }

    [Required] 
    public int ErrorColumnNumber { get; set; }

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? UserAgent { get; set; }
    public string? BrowserName { get; set; }
    public string? BrowserVersion { get; set; }
    public string? DeviceType { get; set; }
    
    // 错误上下文
    public string? ComponentStack { get; set; } // React组件堆栈
    public Dictionary<string, object>? ErrorContext { get; set; } // 额外上下文信息
    public string? UserActions { get; set; } // 导致错误的用户操作序列

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// Promise拒绝错误DTO
/// </summary>
public class PromiseRejectionDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public string? UserId { get; set; }

    [Required]
    public string ReasonMessage { get; set; } = string.Empty;

    public string? ReasonStack { get; set; }
    public string? ReasonType { get; set; }

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? UserAgent { get; set; }
    public string? PromiseChain { get; set; } // Promise链信息
    public Dictionary<string, object>? RejectionContext { get; set; }

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// 网络错误DTO
/// </summary>
public class NetworkErrorDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public string? UserId { get; set; }

    [Required]
    public string RequestUrl { get; set; } = string.Empty;

    [Required]
    public string HttpMethod { get; set; } = string.Empty; // GET, POST, PUT, DELETE

    public int? StatusCode { get; set; }
    public string? StatusText { get; set; }
    public string? ErrorType { get; set; } // timeout, abort, error, etc.
    public string? ErrorMessage { get; set; }
    
    // 请求信息
    public Dictionary<string, string>? RequestHeaders { get; set; }
    public string? RequestBody { get; set; } // 截断的请求体
    public long? RequestSize { get; set; }
    
    // 响应信息
    public Dictionary<string, string>? ResponseHeaders { get; set; }
    public string? ResponseBody { get; set; } // 截断的响应体
    public long? ResponseSize { get; set; }
    
    // 性能信息
    public decimal? Duration { get; set; } // 请求耗时（毫秒）

    [Required]
    public string PageUrl { get; set; } = string.Empty;

    public string? UserAgent { get; set; }

    [Required]
    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; }
}

/// <summary>
/// 批量数据处理DTO
/// </summary>
public class BatchDataDto
{
    [Required]
    public string SessionId { get; set; } = string.Empty;

    public List<VitalsMetricDto>? VitalsMetrics { get; set; }
    public List<CustomMetricDto>? CustomMetrics { get; set; }
    public List<ErrorLogDto>? GeneralErrors { get; set; }
    public List<JavaScriptErrorDto>? JavaScriptErrors { get; set; }
    public List<PromiseRejectionDto>? PromiseRejections { get; set; }
    public List<NetworkErrorDto>? NetworkErrors { get; set; }
    public List<PageViewDto>? PageViews { get; set; }
    public List<ClickEventDto>? ClickEvents { get; set; }
    public List<ScrollDepthDto>? ScrollEvents { get; set; }
    public List<FormInteractionDto>? FormInteractions { get; set; }
    public List<UserInteractionDto>? GeneralInteractions { get; set; }

    [Required]
    [Range(1, long.MaxValue)]
    public long BatchTimestamp { get; set; }

    public string? BatchId { get; set; }
    public int? BatchSequence { get; set; } // 批次序号
}

/// <summary>
/// 大批量数据处理DTO
/// </summary>
public class BulkDataDto
{
    [Required]
    public string OperationType { get; set; } = string.Empty; // import, sync, migrate

    [Required]
    public List<object> DataItems { get; set; } = new();

    public string? SourceSystem { get; set; }
    public string? TargetSystem { get; set; }
    public bool EnableValidation { get; set; } = true;
    public bool EnableTransactions { get; set; } = true;
    public int BatchSize { get; set; } = 1000;
    public int MaxRetries { get; set; } = 3;

    [Range(1, long.MaxValue)]
    public long Timestamp { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}

/// <summary>
/// 性能指标摘要
/// </summary>
public class PerformanceMetricSummary
{
    public string MetricName { get; set; } = string.Empty;
    public decimal AverageValue { get; set; }
    public decimal MinValue { get; set; }
    public decimal MaxValue { get; set; }
    public string Rating { get; set; } = string.Empty;
}

/// <summary>
/// 错误摘要
/// </summary>
public class ErrorSummary
{
    public string ErrorType { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public int Count { get; set; }
    public string Severity { get; set; } = string.Empty;
    public DateTime LastOccurrence { get; set; }
}

/// <summary>
/// 页面分析数据DTO
/// </summary>
public class PageAnalysisDto
{
    public string PageUrl { get; set; } = string.Empty;
    public string PageTitle { get; set; } = string.Empty;
    public int ViewCount { get; set; }
    public int UniqueVisitors { get; set; }
    public decimal AverageLoadTime { get; set; }
    public decimal BounceRate { get; set; }
    public decimal AverageSessionDuration { get; set; }
    public List<PerformanceMetricSummary> PerformanceMetrics { get; set; } = new();
    public List<ErrorSummary> TopErrors { get; set; } = new();
    public Dictionary<string, int> DeviceBreakdown { get; set; } = new();
    public Dictionary<string, int> BrowserBreakdown { get; set; } = new();
    public List<string> TopReferrers { get; set; } = new();
}

/// <summary>
/// 用户行为分析数据DTO
/// </summary>
public class UserAnalysisDto
{
    public string? UserId { get; set; }
    public int TotalSessions { get; set; }
    public int TotalPageViews { get; set; }
    public decimal AverageSessionDuration { get; set; }
    public List<string> TopPages { get; set; } = new();
    public List<InteractionSummary> TopInteractions { get; set; } = new();
    public List<DeviceInfo> Devices { get; set; } = new();
    public DateTime FirstVisit { get; set; }
    public DateTime LastVisit { get; set; }
    public Dictionary<string, int> HourlyActivity { get; set; } = new();
    public Dictionary<string, decimal> PerformanceProfile { get; set; } = new();
}

/// <summary>
/// 实时监控数据DTO
/// </summary>
public class RealtimeDataDto
{
    public int ActiveSessions { get; set; }
    public int CurrentPageViews { get; set; }
    public List<LiveMetric> LiveMetrics { get; set; } = new();
    public List<RecentError> RecentErrors { get; set; } = new();
    public List<ActivePage> ActivePages { get; set; } = new();
    public SystemStatus SystemStatus { get; set; } = new();
    public long Timestamp { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}

/// <summary>
/// 实时指标
/// </summary>
public class LiveMetric
{
    public string Name { get; set; } = string.Empty;
    public decimal Value { get; set; }
    public string Unit { get; set; } = string.Empty;
    public string Trend { get; set; } = string.Empty; // up, down, stable
    public long Timestamp { get; set; }
}

/// <summary>
/// 最近错误
/// </summary>
public class RecentError
{
    public string ErrorType { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public string PageUrl { get; set; } = string.Empty;
    public string Severity { get; set; } = string.Empty;
    public int Count { get; set; }
    public long Timestamp { get; set; }
}

/// <summary>
/// 活跃页面
/// </summary>
public class ActivePage
{
    public string Url { get; set; } = string.Empty;
    public string Title { get; set; } = string.Empty;
    public int ActiveUsers { get; set; }
    public decimal AverageLoadTime { get; set; }
}

/// <summary>
/// 系统状态
/// </summary>
public class SystemStatus
{
    public string Status { get; set; } = "healthy";
    public decimal CpuUsage { get; set; }
    public decimal MemoryUsage { get; set; }
    public int DatabaseConnections { get; set; }
    public List<ServiceHealth> Services { get; set; } = new();
}

/// <summary>
/// 服务健康状态
/// </summary>
public class ServiceHealth
{
    public string ServiceName { get; set; } = string.Empty;
    public string Status { get; set; } = string.Empty;
    public long ResponseTime { get; set; }
    public string? LastError { get; set; }
}

/// <summary>
/// 监控配置DTO
/// </summary>
public class MonitoringConfigDto
{
    public bool Enabled { get; set; } = true;
    public decimal SamplingRate { get; set; } = 1.0m;
    public int BatchSize { get; set; } = 10;
    public int MaxErrorsPerMinute { get; set; } = 100;
    public int DataRetentionDays { get; set; } = 90;
    public bool EnableRealTimeNotifications { get; set; } = true;
    public Dictionary<string, decimal> PerformanceThresholds { get; set; } = new();
    public List<string> ErrorFilters { get; set; } = new();
    public Dictionary<string, object> CustomSettings { get; set; } = new();
}

/// <summary>
/// 交互摘要
/// </summary>
public class InteractionSummary
{
    public string ActionType { get; set; } = string.Empty;
    public string Element { get; set; } = string.Empty;
    public int Count { get; set; }
    public decimal AverageLatency { get; set; }
}

/// <summary>
/// 设备信息
/// </summary>
public class DeviceInfo
{
    public string DeviceType { get; set; } = string.Empty;
    public string Browser { get; set; } = string.Empty;
    public string OS { get; set; } = string.Empty;
    public string Resolution { get; set; } = string.Empty;
    public int SessionCount { get; set; }
    public DateTime LastUsed { get; set; }
}
